﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum AttackType
{
    Normal,
    Crit,
    Skill,
    Miss,
    Health
}

public class FishAttack : MonoBehaviour {

    private GameObject fishGo;
    private FishData fdata;
    private FishView fv;
    private List<GameObject> enemyGoList = new List<GameObject>();
    private EmBattle emBattle;

    public GameObject EnemyGo;
    public AttackType attackType;
    

    private void Awake()
    {
        fishGo = this.gameObject;
        fv = fishGo.GetComponent<FishView>();

    }

    //BUFF响应
    public void BuffEffect()
    {
        fdata = fv.fdata;
        emBattle = fishGo.GetComponent<EmBattle>();

        if (fdata.SkillBuff == null) return;

        for (int i=0;i< fdata.SkillBuff.Count; i++)
        {
            fdata.BuffChange(fdata.SkillBuff[i]);
        }
    }

    //寻找目标
    public void LookTarget()
    {
        BuffEffect();

        string s = fishGo.tag;
        string fishTag = "";

        if (s == "Hero")
        {
            fishTag = "p";
        }
        else
        {
            fishTag = "e";
        }
        //print(fv.fishPos);
        if (fv.fishPos == 1 || fv.fishPos == 2 || fv.fishPos == 3)
        {
            int[] j = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            for (int i = 0; i < j.Length; i++)
            {
                GameObject go = GameObject.Find(fishTag + j[i]);
                if (go.transform.childCount > 0)
                {
                    EnemyGo = go.transform.GetChild(0).gameObject;
                    if (EnemyGo != null)
                    {
                        Attack(EnemyGo);
                    }
                    return;
                }
            }
        }
        else if (fv.fishPos == 4 || fv.fishPos == 5 || fv.fishPos == 6)
        {
            int[] j = { 4, 5, 6, 7, 8, 9, 1, 2, 3 };

            for (int i = 0; i < j.Length; i++)
            {
                GameObject go = GameObject.Find(fishTag + j[i]);
                if (go.transform.childCount > 0)
                {
                    EnemyGo = go.transform.GetChild(0).gameObject;
                    if (EnemyGo != null)
                    {
                        Attack(EnemyGo);
                    }
                    return;
                }
            }
        }
        else if (fv.fishPos == 7 || fv.fishPos == 8 || fv.fishPos == 9)
        {
            int[] j = { 7, 8, 9, 4, 5, 6, 1, 2, 3 };

            for (int i = 0; i < j.Length; i++)
            {
                GameObject go = GameObject.Find(fishTag + j[i]);
                if (go.transform.childCount > 0)
                {
                    EnemyGo = go.transform.GetChild(0).gameObject;
                    if (EnemyGo != null)
                    {
                        Attack(EnemyGo);
                    }
                    return;
                }
            }
        }
    }

    //攻击的初始化，判断普通攻击还是技能攻击
    void Attack(GameObject enemyGo)
    {
        if (fdata.Mana >= 100)
        {
            switch (fdata.SkillType)
            {
                case 4:
                    BuffAttack(1001);
                    break;

                case 5:
                    BuffAttack(1002);
                    break;

                case 6:
                    BuffAttack(1003);
                    break;

                default:
                    SkillAttack(enemyGo);
                    break;
            }
        }
        else
        {
            NormalAttack(enemyGo);
        }
    }

    //普通攻击
    void NormalAttack(GameObject enemyGo)
    {
        //计算伤害
        int damage = DamaCalcu(0f, enemyGo);

        //计算敌人血量
        enemyGo.GetComponent<FishView>().fdata.HpChange(-damage);

        //攻击表现
        fv.Attack(damage, enemyGo);

        //增加怒气
        if (damage > 0)
        {
            fdata.ManaChange(25);
            enemyGo.GetComponent<FishView>().fdata.ManaChange(25);
            print(fdata.FishID1 + " 对 " + enemyGo.name + "普攻 " + damage + " 点伤害");
        }
        else
        {
            print("未命中");
        }
        
    }
    //技能攻击
    void SkillAttack(GameObject enemyGo)
    {
        //根据技能类型来寻找目标
        SkillTarget(enemyGo);

        //超出的魔法转换为技能系数
        float skillIn = (fdata.Mana - 100f) / 100f;
        if (skillIn <= 0) skillIn = 0;
        
        if (enemyGoList.Count == 1)
        {
            //伤害计算
            int damage = DamaCalcu(1.5f + skillIn, enemyGoList[0]);
            //计算敌人血量
            enemyGoList[0].GetComponent<FishView>().fdata.HpChange(-damage);
            //攻击表现
            fv.Attack(damage, enemyGoList[0]);

            Debug.LogWarning(fdata.FishID1 + " 对 " + enemyGoList[0].name + "【技能】 " + damage + " 点伤害");
        }
        else if (enemyGoList.Count > 1)
        {
            //伤害计算
            ArrayList damageList = new ArrayList();
            for (int i = 0; i < enemyGoList.Count; i++)
            {
                int tempdamage = DamaCalcu(1.2f + skillIn, enemyGoList[i]);
                damageList.Add(tempdamage);
                //计算敌人血量
                enemyGoList[i].GetComponent<FishView>().fdata.HpChange(-tempdamage);
                Debug.LogWarning(fdata.FishID1 + " 对 " + enemyGoList[i].name + "【技能】 " + tempdamage + " 点伤害");
            }
            //攻击表现
            fv.Attack(damageList, enemyGoList);

        }
        else if (enemyGoList.Count <1)
        {
            Debug.Log("错了啊，没有目标了");
            return;
        }

        //减去怒气
        fdata.ManaChange(-1000);

        //清除目标列表
        enemyGoList.Clear();
    }
    //BUFF
    void BuffAttack(int buffID)
    {
        emBattle.AddBuffData(buffID);

        //减去怒气
        fdata.ManaChange(-1000);

        StartCoroutine(EndRound());
    }
    
    //计算伤害
    int DamaCalcu(float skillinjury, GameObject enemyGo)
    {
        int damage = 0;
        FishData enemyFdata = enemyGo.GetComponent<FishView>().fdata;
        //计算幸运攻击
        float LuckyAtk = fdata.Atk * (1 + Random.Range(0f, fdata.Lucky) / 100f);
        //计算命中率，技能无视命中率
        float hitPer = 1;
        if (skillinjury == 0)
        {
            attackType = AttackType.Normal;
            hitPer = Mathf.Max((fdata.Hit + 100f - enemyFdata.Dodge) / 100f, 0.5f);
        }
        else
        {
            attackType = AttackType.Skill;
        }
        float hitPerRandom = Random.Range(0f, 1f);
        if (hitPer >= hitPerRandom)
        {
            //计算暴击率，技能无暴击
            float critPer = 0;
            if(skillinjury == 0) critPer = Mathf.Min(Mathf.Max((fdata.Crit - enemyFdata.Anti_crit) / 100f, 0f), 0.99f);
            float critPerRandom = Random.Range(0f, 1f);
            //计算暴击倍率
            float critMultiple = 0f;
            if (critPer >= critPerRandom)
            {
                attackType = AttackType.Crit;
                critMultiple = Mathf.Min(Mathf.Max((fdata.Crit_multiple + 100f - enemyFdata.Crit_derate) / 100f, 0.25f), 2f);
            }
            //计算伤害 
            float damage1 = LuckyAtk * (1 + skillinjury) - enemyFdata.Def;
            float damage2 = damage1 * (1 + critMultiple);

            //计算克制系数
            float eleAttri = 0f;
            if (enemyFdata.WeaponAttri == 0 && fdata.WeaponAttri == 2)
            {
                eleAttri = 0.2f;
            }
            else if (enemyFdata.WeaponAttri == 2 && fdata.WeaponAttri == 1)
            {
                eleAttri = 0.2f;
            }
            else if (enemyFdata.WeaponAttri == 1 && fdata.WeaponAttri == 0)
            {
                eleAttri = 0.2f;
            }

            float damage3 = damage2 * (1f + eleAttri + fdata.Injury / 100f - enemyFdata.Injury_free / 100f);

            damage = (int)Mathf.Max(damage3, fdata.Atk * 0.1f);
        }
        else
        {
            //未命中
            damage = 0;
            attackType = AttackType.Miss;
        }
        return damage;
    }

    //获取技能目标
    void SkillTarget(GameObject enemyGo)
    {
        switch (fdata.SkillType)
        {
            case 0:
                enemyGoList.Add(enemyGo);
                break;
            case 1:
                if (CalcaPos(1, 2, 3, enemyGo)) return;
                if (CalcaPos(4, 5, 6, enemyGo)) return;
                if (CalcaPos(7, 8, 9, enemyGo)) return;
                break;
            case 2:
                if (CalcaPos(1, 4, 7, enemyGo)) return;
                if (CalcaPos(2, 5, 8, enemyGo)) return;
                if (CalcaPos(3, 6, 9, enemyGo)) return;
                break;
            case 3:
                RandomTarget();
                break;

        }
    }

    //根据提供的坐标将符合条件的鱼装入enemyGoList;
    bool CalcaPos(int pos1,int pos2,int pos3,GameObject enemyGo)
    {
        int pos = enemyGo.GetComponent<FishView>().fishPos;
        if (pos == pos1 || pos == pos2 || pos == pos3)
        {
            if (gameObject.tag == "Enemy")
            {
                for (int i = 0; i < GameControl._instance.heroFish.Count; i++)
                {
                    int enemyPos = GameControl._instance.heroFish[i].GetComponent<FishView>().fishPos;
                    if (enemyPos == pos1 || enemyPos == pos2 || enemyPos == pos3)
                    {
                        enemyGoList.Add(GameControl._instance.heroFish[i]);
                    }
                }
            }
            else
            {
                for (int i = 0; i < GameControl._instance.enemyFish.Count; i++)
                {
                    int enemyPos = GameControl._instance.enemyFish[i].GetComponent<FishView>().fishPos;
                    if (enemyPos == pos1 || enemyPos == pos2 || enemyPos == pos3)
                    {
                        enemyGoList.Add(GameControl._instance.enemyFish[i]);
                    }
                }
            }
        }
        if (enemyGoList.Count != 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //随机攻击目标
    void RandomTarget()
    {
        int count = 0;
        

        if (gameObject.tag == "Hero")
        {
            List<GameObject> tempHeroFish = GameControl._instance.enemyFish;

            count = tempHeroFish.Count;

            if (count > 3)
            {
                count = 3;
            }


            GameControl._instance.RandomSortList(tempHeroFish);
            for (int i = 0; i < count; i++)
            {
                enemyGoList.Add(tempHeroFish[i]);
            }

            tempHeroFish = null;
        }
        else
        {
            List<GameObject> tempHeroFish = GameControl._instance.heroFish;

            count = tempHeroFish.Count;

            if (count > 3)
            {
                count = 3;
            }

            GameControl._instance.RandomSortList(tempHeroFish);
            for (int i = 0; i < count; i++)
            {
                enemyGoList.Add(tempHeroFish[i]);
            }

            tempHeroFish = null;
        }
    }

    //结束回合
    IEnumerator EndRound()
    {
        yield return new WaitForSeconds(1f);

        GameControl._instance.mState = OperatorState.Round;
        GameControl._instance.ButtleTurn();
    }
}
